package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.pojo.TbTypeTemplateExample;
import com.dongdongshop.service.TypeTemplateService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class TypeTemplateServiceImpl implements TypeTemplateService {
    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Override
    public List<TbTypeTemplate> listTypeTemplates(String name) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        if (!"".equals(name) && name != null){
            TbTypeTemplateExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+name+"%");
        }
        return tbTypeTemplateMapper.selectByExample(example);
    }

    @Override
    public void addTemplate(String name, String specIds, String brandIds, String customAttributeItems) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        tbTypeTemplate.setName(name);
        tbTypeTemplate.setSpecIds(specIds);
        tbTypeTemplate.setBrandIds(brandIds);
        tbTypeTemplate.setCustomAttributeItems(customAttributeItems);

        tbTypeTemplateMapper.insertSelective(tbTypeTemplate);
    }

    @Override
    public void delTemplates(List<Long> ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbTypeTemplateMapper.deleteByExample(example);
    }

    @Override
    public TbTypeTemplate selectTemplateById(Long id) {
        return tbTypeTemplateMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateTemplate(String name, String specIds, String brandIds, String customAttributeItems, Long id) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        tbTypeTemplate.setName(name);
        tbTypeTemplate.setSpecIds(specIds);
        tbTypeTemplate.setBrandIds(brandIds);
        tbTypeTemplate.setCustomAttributeItems(customAttributeItems);
        tbTypeTemplate.setId(id);
        tbTypeTemplateMapper.updateByPrimaryKeySelective(tbTypeTemplate);
    }
}
